package com.bestcem.xm.txtanls.controller.convert;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.txtanls.enums.EmotionTagEnum;
import com.bestcem.xm.txtanls.service.dto.chart.*;
import com.bestcem.xm.txtanls.util.ChartUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * DTO到VO转换类
 * @author jy.zhao
 * @version 2.0
 * @date 2021/8/5 22:17
 **/
public class ChartExportConvert {

    /**
     * excel的响应头
     */
    public static final String EXCEL_RESPONSE_HEADER_KEY = "Content-disposition";
    public static String EXCEL_RESPONSE_HEADER_VALUE = "attachment;filename=%s.xlsx";
    /**
     * 图表导出excel时文件名称
     */
    public static final String EMOTION_DETAIL_FILE_NAME = "总体情感细分";
    public static final String EMOTION_TAG_FEEDBACK_FILE_NAME = "客户反馈总数";
    public static final String EMOTION_TAG_POSITIVE_FILE_NAME = "正面情感";
    public static final String EMOTION_TAG_NEGATIVE_FILE_NAME = "负面情感";
    public static final String EMOTION_TREND_FILE_NAME = "总体情感趋势";
    public static final String VIEWPOINT_STAT_FILE_NAME = "观点情感看板";
    public static final String VIEWPOINT_TOP_FILE_NAME = "TOP5话题";
    public static final String TOPIC_REFERENCE_FILE_NAME = "话题提及率变化";
    public static final String VIEWPOINT_EVALUATE_FILE_NAME = "观点好评率变化";
    public static final String WORD_CLOUD_FILE_NAME = "词频统计";
    public static final String TOPIC_RELATION_FILE_NAME = "话题关联分析";
    public static final String TOPIC_REFERENCE_TREND_FILE_NAME = "话题提及率变化趋势--name";
    public static final String VIEWPOINT_EVALUATE_TREND_FILE_NAME = "观点情感分布变化趋势--name";
    public static final String TOPIC_ANALYSIS_FILE_NAME = "关键话题指标";

    /**
     * 图表导出excel时excel表头
     */
    private static final List<Object> EMOTION_DETAIL_FILE_HEADER = CollUtil.newArrayList("", "反馈总数", "正面", "中性", "混合", "负面");
    private static final List<Object> EMOTION_TAG_FEEDBACK_FILE_HEADER = CollUtil.newArrayList("客户反馈总数", "环比(%)", "上期");
    private static final List<Object> EMOTION_TAG_POSITIVE_FILE_HEADER = CollUtil.newArrayList("正面情感", "占比(%)", "较上期(%)");
    private static final List<Object> EMOTION_TAG_NEGATIVE_FILE_HEADER = CollUtil.newArrayList("负面情感", "占比(%)", "较上期(%)");
    private static final List<Object> EMOTION_TREND_FILE_HEADER = CollUtil.newArrayList("", "反馈总数", "正面", "中性", "混合", "负面");
    private static final List<Object> VIEWPOINT_STAT_FILE_HEADER = CollUtil.newArrayList("一级观点组", "二级观点", "提及量", "提及率(%)", "正面提及量", "正面提及率(%)", "中性提及量", "中性提及率(%)", "负面提及量", "负面提及率(%)");
    private static final List<Object> VIEWPOINT_TOP_FILE_HEADER = CollUtil.newArrayList("好评率TOP5话题", "好评率(%)", "差评率TOP5话题", "差评率(%)");
    private static final List<Object> WORD_CLOUD_FILE_HEADER = CollUtil.newArrayList("关键词", "词频(%)");
    private static final List<Object> TOPIC_RELATION_FILE_HEADER = CollUtil.newArrayList("", "占比(%)");
    private static final List<Object> TOPIC_REFERENCE_TREND_FILE_HEADER = CollUtil.newArrayList("", "提及率");
    private static final List<Object> VIEWPOINT_GROUP_EVALUATE_TREND_FILE_HEADER = CollUtil.newArrayList("", "正面(%)", "中性(%)", "混合(%)", "负面(%)");
    private static final List<Object> VIEWPOINT_EVALUATE_TREND_FILE_HEADER = CollUtil.newArrayList("", "正面(%)", "中性(%)", "负面(%)");
    private static final List<Object> TOPIC_ANALYSIS_FILE_HEADER = CollUtil.newArrayList("指标", "话题", "本期提及", "上期提及", "环比(%)");

    /**
     * 列明定义
     */
    private static final String CURRENT_PERIOD_TXT = "本期(%s)";
    private static final String PREVIOUS_PERIOD_TXT = "上期(%s)";
    private static final String COMPARE_PREVIOUS_PERIOD_TXT = "比上期(%s)";
    private static final String GROWTH_RATIO_TXT = "环比(%)";

    /**
     * excel空数据定义
     */
    private static final String EMPTY_DATA = "-";


    /**
     * 总体情感细分emotionDetailDTO转换为excel数据
     * @param emotionDetailDTO 参数
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 14:42
     **/
    public static List<List<Object>> emotionDetailToExcelData(EmotionDetailDTO emotionDetailDTO, Integer interval) {
        List<List<Object>> excelData = new ArrayList<>();
        EmotionDTO currentPeriod = emotionDetailDTO.getCurEmotion();
        EmotionDTO ringGrowth = emotionDetailDTO.getGrowth();
        EmotionDTO ringRatio = emotionDetailDTO.getGrowthRatio();
        String curPeriodTime = ChartUtil.formatPeriodTime(emotionDetailDTO.getCurPeriod(), interval);
        String lastPeriodTime = ChartUtil.formatPeriodTime(emotionDetailDTO.getLastPeriod(), interval);
        List<Object> firstRow = Arrays.asList(String.format(CURRENT_PERIOD_TXT, curPeriodTime), currentPeriod.getTotal(), currentPeriod.getPositive(), currentPeriod.getNeutral(), currentPeriod.getMixture(), currentPeriod.getNegative());
        List<Object> secondRow = Arrays.asList(String.format(COMPARE_PREVIOUS_PERIOD_TXT, lastPeriodTime), ringGrowth.getTotal(), ringGrowth.getPositive(), ringGrowth.getNeutral(), ringGrowth.getMixture(), ringGrowth.getNegative());
        List<Object> thirdRow = Arrays.asList(GROWTH_RATIO_TXT, ringRatio.getTotal(), ringRatio.getPositive(), ringRatio.getNeutral(), ringRatio.getMixture(), ringRatio.getNegative());
        excelData.add(EMOTION_DETAIL_FILE_HEADER);
        excelData.add(firstRow);
        excelData.add(secondRow);
        excelData.add(thirdRow);
        return excelData;
    }

    /**
     * 总体情感标签emotionDetailDTO转换为excel数据
     * @param emotionDetailDTO 参数
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 14:42
     **/
    public static List<List<Object>> emotionTagToExcelData(EmotionDetailDTO emotionDetailDTO, Integer type) {
        List<List<Object>> excelData = new ArrayList<>();
        List<Object> firstRow;
        EmotionDTO currentPeriod = emotionDetailDTO.getCurEmotion();
        EmotionDTO ringGrowth = emotionDetailDTO.getGrowth();
        EmotionDTO ringRatio = emotionDetailDTO.getGrowthRatio();
        if(type == EmotionTagEnum.CUSTOMER_FEEDBACK.getIndex()) {
            excelData.add(EMOTION_TAG_FEEDBACK_FILE_HEADER);
            firstRow = Arrays.asList(currentPeriod.getTotal(), ringRatio.getTotal(), currentPeriod.getTotal() - ringGrowth.getTotal());
        } else if(type == EmotionTagEnum.POSITIVE_EMOTION.getIndex()) {
            excelData.add(EMOTION_TAG_POSITIVE_FILE_HEADER);
            firstRow = Arrays.asList(currentPeriod.getPositive(), currentPeriod.getPosRatio(), ringGrowth.getPosRatio());
        } else {
            excelData.add(EMOTION_TAG_NEGATIVE_FILE_HEADER);
            firstRow = Arrays.asList(currentPeriod.getNegative(), currentPeriod.getNegRatio(), ringGrowth.getNegRatio());
        }
        excelData.add(firstRow);
        return excelData;
    }

    /**
     * 总体情感细分EmotionDTO转excel数据
     * @param dtoList 参数
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 14:42
     **/
    public static List<List<Object>> emotionToExcelData(List<EmotionDTO> dtoList, Integer interval) {
        List<List<Object>> excelData = new ArrayList<>();
        excelData.add(EMOTION_TREND_FILE_HEADER);
        for (EmotionDTO vo:dtoList) {
            List<Object> row = Arrays.asList(ChartUtil.formatPeriodTime(vo.getPeriodTime(), interval), vo.getTotal(), vo.getPositive(), vo.getNeutral(), vo.getMixture(), vo.getNegative());
            excelData.add(row);
        }
        return excelData;
    }

    /**
     * 观点组统计 vo转换为excelData
     * @param groupStatDTOList 参数
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 14:51
     **/
    public static List<List<Object>> viewpointGroupStatToExcelData(List<ViewpointGroupStatDTO> groupStatDTOList) {
        List<List<Object>> excelData = new ArrayList<>();
        excelData.add(VIEWPOINT_STAT_FILE_HEADER);
        for(ViewpointGroupStatDTO groupStatDTO:groupStatDTOList) {
            ViewpointStatDTO viewpointGroupStat = groupStatDTO.getViewpointGroupStat();
            List<Object> groupStat = Arrays.asList(viewpointGroupStat.getName(), EMPTY_DATA, viewpointGroupStat.getNum(), viewpointGroupStat.getRatio(),
                    viewpointGroupStat.getPosNum(), viewpointGroupStat.getPosRatio(), viewpointGroupStat.getNeuNum(), viewpointGroupStat.getNeuRatio(),
                    viewpointGroupStat.getNegNum(), viewpointGroupStat.getNegRatio());
            excelData.add(groupStat);
            for(ViewpointStatDTO viewpointStatDTO:groupStatDTO.getViewpointStats()) {
                List<Object> viewpointStat = Arrays.asList(viewpointGroupStat.getName(), viewpointStatDTO.getName(), viewpointStatDTO.getNum(), viewpointStatDTO.getRatio(),
                        viewpointStatDTO.getPosNum(), viewpointStatDTO.getPosRatio(), viewpointStatDTO.getNeuNum(), viewpointStatDTO.getNeuRatio(),
                        viewpointStatDTO.getNegNum(), viewpointStatDTO.getNegRatio());
                excelData.add(viewpointStat);
            }
        }
        return excelData;
    }

    /**
     * 观点top5
     * @param viewpointTopDTO
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/11/15 15:14
     **/
    public static List<List<Object>> viewpointTopToExcelData(ViewpointTopDTO viewpointTopDTO) {
        List<List<Object>> excelData = new ArrayList<>();
        excelData.add(VIEWPOINT_TOP_FILE_HEADER);
        List<ViewpointEvaluationTopDTO> badEvaluations = viewpointTopDTO.getBadEvaluations();
        List<ViewpointEvaluationTopDTO> goodEvaluations = viewpointTopDTO.getGoodEvaluations();
        for (int i = 0; i < 5; i++) {

            ViewpointEvaluationTopDTO badEvaluation = null;
            ViewpointEvaluationTopDTO goodEvaluation = null;
            if(i < badEvaluations.size()) {
                badEvaluation = badEvaluations.get(i);
            }
            if(i < goodEvaluations.size()) {
                goodEvaluation = goodEvaluations.get(i);
            }
            excelData.add(Arrays.asList(Objects.isNull(goodEvaluation)?"":goodEvaluation.getName(), Objects.isNull(goodEvaluation)?"":goodEvaluation.getRatio(),
                    Objects.isNull(badEvaluation)?"":badEvaluation.getName(), Objects.isNull(badEvaluation)?"":badEvaluation.getRatio()));
        }
        return excelData;
    }

    /**
     * 话题提及率变化 TopicReferenceRateVO转换为excelData
     * @param dto 参数
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 15:00
     **/
    public static List<List<Object>> topicReferenceRateToExcelData(TopicReferenceRateDTO dto, Integer interval) {
        List<List<Object>> excelData = new ArrayList<>();
        String curPeriodTime = ChartUtil.formatPeriodTime(dto.getCurPeriod(), interval);
        String lastPeriodTime = ChartUtil.formatPeriodTime(dto.getLastPeriod(), interval);
        excelData.add(Arrays.asList("", String.format(PREVIOUS_PERIOD_TXT, lastPeriodTime), String.format(CURRENT_PERIOD_TXT, curPeriodTime)));
        for(ReferenceRateDTO referenceRateDTO:dto.getRefRates()) {
            List<Object> referenceRate = Arrays.asList(referenceRateDTO.getName(), toPercent2(referenceRateDTO.getLastRatio()), toPercent2(referenceRateDTO.getCurRatio()));
            excelData.add(referenceRate);
        }
        return excelData;
    }

    /**
     * 观点评价变化 ViewPointEvaluateVO转换为excelData
     * @param dto 参数
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 15:00
     **/
    public static List<List<Object>> viewPointEvaluateToExcelData(ViewPointEvaluateDTO dto, Integer interval) {
        List<List<Object>> excelData = new ArrayList<>();
        String curPeriodTime = ChartUtil.formatPeriodTime(dto.getCurPeriod(), interval);
        String lastPeriodTime = ChartUtil.formatPeriodTime(dto.getLastPeriod(), interval);
        excelData.add(Arrays.asList("", String.format(PREVIOUS_PERIOD_TXT, lastPeriodTime), String.format(CURRENT_PERIOD_TXT, curPeriodTime)));
        for(ViewPointEvaluateRatioDTO viewPointEvaluateRatioDTO:dto.getViewRatios()) {
            List<Object> referenceRate = Arrays.asList(viewPointEvaluateRatioDTO.getName(), toPercent(viewPointEvaluateRatioDTO.getLastRatio()), toPercent(viewPointEvaluateRatioDTO.getCurRatio()));
            excelData.add(referenceRate);
        }
        return excelData;
    }

    /**
     * 词云图 workCloudDataDTOList转为excelData
     * @param workCloudDataDTOList 参数
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 15:06
     **/
    public static List<List<Object>> workCloudDataToExcelData(List<WorkCloudDataDTO> workCloudDataDTOList) {
        List<List<Object>> excelData = new ArrayList<>();
        excelData.add(WORD_CLOUD_FILE_HEADER);
        for(WorkCloudDataDTO workCloudDataDTO:workCloudDataDTOList) {
            List<Object> referenceRate = Arrays.asList(workCloudDataDTO.getKeyword(), workCloudDataDTO.getCount());
            excelData.add(referenceRate);
        }
        return excelData;
    }

    /**
     * 话题关联转换 topicRelationDTOList转换为excelData
     * @param topicRelationDTOList 参数
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 15:08
     **/
    public static List<List<Object>> topicRelationToExcelData(List<TopicRelationDTO> topicRelationDTOList) {
        List<List<Object>> excelData = new ArrayList<>();
        excelData.add(TOPIC_RELATION_FILE_HEADER);
        for(TopicRelationDTO topicRelationDTO:topicRelationDTOList) {
            List<Object> topicRelation = Arrays.asList(topicRelationDTO.getName(), topicRelationDTO.getRatio());
            excelData.add(topicRelation);
        }
        return excelData;
    }

    /**
     * 话题提及率变化趋势转换excelData
     * @param topicStatDTOList 参数
     * @param interval 时间间隔
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 15:11
     **/
    public static List<List<Object>> topicStatToExcelData(List<TopicStatDTO> topicStatDTOList, Integer interval) {
        List<List<Object>> excelData = new ArrayList<>();
        excelData.add(TOPIC_REFERENCE_TREND_FILE_HEADER);
        for(TopicStatDTO topicStatDTO:topicStatDTOList) {
            List<Object> topicStat = Arrays.asList(ChartUtil.formatPeriodTime(topicStatDTO.getPeriodTime(), interval), toPercent(topicStatDTO.getRatio()));
            excelData.add(topicStat);
        }
        return excelData;
    }

    /**
     * 话题提及率变化趋势转换excelData
     * @param topicAnalysisDTO
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/11/17 18:07
     **/
    public static List<List<Object>> topicAnalysisToExcelData(TopicAnalysisDTO topicAnalysisDTO) {
        List<List<Object>> excelData = new ArrayList<>();
        excelData.add(TOPIC_ANALYSIS_FILE_HEADER);
        List<TopicTopDTO> topicTopDTOList = topicAnalysisDTO.getTopicTopDTOList();
        TopicHotDTO topicFastDTO = topicAnalysisDTO.getTopicFastDTO();
        TopicHotDTO topicHotDTO = topicAnalysisDTO.getTopicHotDTO();
        TopicHotDTO topicHotLastDTO = topicAnalysisDTO.getTopicHotLastDTO();
        excelData.add(Arrays.asList("本期最热门话题", topicHotDTO.getName(), topicHotDTO.getCurCount(), topicHotDTO.getLastCount(), topicHotDTO.getRingRatio()));
        excelData.add(Arrays.asList("近期热度激增话题", topicFastDTO.getName(), topicFastDTO.getCurCount(), topicFastDTO.getLastCount(), topicFastDTO.getRingRatio()));
        excelData.add(Arrays.asList("上期最热门话题", topicHotLastDTO.getName(), topicHotLastDTO.getCurCount(), topicHotLastDTO.getLastCount(), topicHotLastDTO.getRingRatio()));
        excelData.add(Arrays.asList("", "", "", "", ""));
        excelData.add(Arrays.asList("话题提及率排名", "话题", "提及率(%)"));
        for (int i = 0; i < topicTopDTOList.size(); i++) {
            TopicTopDTO topicTopDTO = topicTopDTOList.get(i);
            excelData.add(Arrays.asList(i+1, topicTopDTO.getName(), topicTopDTO.getRatio()));
        }
        return excelData;
    }

    /**
     * 观点情感分布变化
     * @param dtoList 参数
     * @param interval 时间间隔
     * @return List<List<Object>>
     * @author jy.zhao
     * @date 2021/8/6 15:13
     **/
    public static List<List<Object>> viewpointEvaluateStatToExcelData(List<ViewpointEvaluateStatDTO> dtoList, Integer interval, boolean isGroup) {
        List<List<Object>> excelData = new ArrayList<>();
        if(isGroup) {
            excelData.add(VIEWPOINT_GROUP_EVALUATE_TREND_FILE_HEADER);
            for(ViewpointEvaluateStatDTO viewpointEvaluateStatDTO:dtoList) {
                List<Object> viewpointEvaluateStat = Arrays.asList(ChartUtil.formatPeriodTime(viewpointEvaluateStatDTO.getPeriodTime(), interval), viewpointEvaluateStatDTO.getPositive(),
                        viewpointEvaluateStatDTO.getNeutral(), viewpointEvaluateStatDTO.getMix(), viewpointEvaluateStatDTO.getNegative());
                excelData.add(viewpointEvaluateStat);
            }
        } else {
            excelData.add(VIEWPOINT_EVALUATE_TREND_FILE_HEADER);
            for(ViewpointEvaluateStatDTO viewpointEvaluateStatDTO:dtoList) {
                List<Object> viewpointEvaluateStat = Arrays.asList(ChartUtil.formatPeriodTime(viewpointEvaluateStatDTO.getPeriodTime(), interval), viewpointEvaluateStatDTO.getPositive(),
                        viewpointEvaluateStatDTO.getNeutral(), viewpointEvaluateStatDTO.getNegative());
                excelData.add(viewpointEvaluateStat);
            }
        }
        return excelData;
    }

    /**
     * 数字转换为百分比
     * @param number 数字
     * @return String
     * @author jy.zhao
     * @date 2021/8/6 18:19
     **/
    private static String toPercent(Integer number){
        String numberString = String.valueOf(number);
        return numberString + "%";
    }

    /**
     * 数字转换为百分比
     * @param number 数字
     * @return String
     * @author jy.zhao
     * @date 2021/8/6 18:19
     **/
    private static String toPercent2(BigDecimal number){
        String numberString = number.toString();
        return numberString + "%";
    }
}
